package com.lanyou.esb.cook.proxy.entity;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.lanyou.esb.cook.proxy.common.Constants;
import com.lanyou.esb.cook.proxy.common.DateUtils;

public class Token {
	/** 接口令牌表ID **/
	private String id;

	/** 接口表ID **/
	private String ifId;

	/** 接口类型 **/
	private String ifType;

	/** 接口名称 **/
	private String ifName;

	/** 接口编码 **/
	private String ifCode;

	/** 接口版本 **/
	private String ifVersion;

	/** 密钥 **/
	private String appKey;

	/** 密钥 有效期 **/
	private Date keyInvalidDate;

	/** 接口发布方系统ID **/
	private String pubSystemId;

	/** 接口发布方系统编码 **/
	private String pubSystemCode;

	/** 接口调用方系统ID **/
	private String callSystemId;

	/** 接口调用方系统编码 **/
	private String callSytemCode;

	/** 联系电话 **/
	private String linkTel;

	/** 令牌申请时间 **/
	private Date applyDate;

	/** 令牌有效时长 **/
	private int invalidTimes;

	/** 申请账号 **/
	private String applyUserCode;

	/** 访问令牌 **/
	private String accessToken;

	/** IP白名单 **/
	private String ipWhiteList;

	/** 是否可用 **/
	private String isEnable;

	/** 创建人 **/
	private String creator;

	/** 创建时间 **/
	private Date createdDate;

	/** 最后更新人员 **/
	private String modifier;

	/** 最后更新时间 **/
	private Date lastUpdatedDate;

	/** 当日已申请次数 **/
	private int curApplyQty = 0;

	private String status;

	public boolean updateToken(String accessToken, int invalidTimes) {
		if (StringUtils.isEmpty(accessToken) || invalidTimes < 0) {
			return false;
		}
		this.accessToken = accessToken;
		this.invalidTimes = invalidTimes;
		Date today = new Date();
		if (this.applyDate != null
				&& DateUtils.isSameDay(this.applyDate, today)) {
			this.applyDate = today;
			this.curApplyQty++;
			return true;
		}
		this.applyDate = today;
		this.curApplyQty = 1;
		return true;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIfId() {
		return ifId;
	}

	public void setIfId(String ifId) {
		this.ifId = ifId;
	}

	public String getIfType() {
		return ifType;
	}

	public void setIfType(String ifType) {
		this.ifType = ifType;
	}

	public String getIfName() {
		return ifName;
	}

	public void setIfName(String ifName) {
		this.ifName = ifName;
	}

	public String getIfCode() {
		return ifCode;
	}

	public void setIfCode(String ifCode) {
		this.ifCode = ifCode;
	}

	public String getIfVersion() {
		return ifVersion;
	}

	public void setIfVersion(String ifVersion) {
		this.ifVersion = ifVersion;
	}

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getPubSystemId() {
		return pubSystemId;
	}

	public void setPubSystemId(String pubSystemId) {
		this.pubSystemId = pubSystemId;
	}

	public String getPubSystemCode() {
		return pubSystemCode;
	}

	public void setPubSystemCode(String pubSystemCode) {
		this.pubSystemCode = pubSystemCode;
	}

	public String getCallSystemId() {
		return callSystemId;
	}

	public void setCallSystemId(String callSystemId) {
		this.callSystemId = callSystemId;
	}

	public String getCallSytemCode() {
		return callSytemCode;
	}

	public void setCallSytemCode(String callSytemCode) {
		this.callSytemCode = callSytemCode;
	}

	public String getLinkTel() {
		return linkTel;
	}

	public void setLinkTel(String linkTel) {
		this.linkTel = linkTel;
	}

	public Date getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(Date applyDate) {
		this.applyDate = applyDate;
	}

	public int getInvalidTimes() {
		return invalidTimes;
	}

	public void setInvalidTimes(int invalidTimes) {
		this.invalidTimes = invalidTimes;
	}

	public String getApplyUserCode() {
		return applyUserCode;
	}

	public void setApplyUserCode(String applyUserCode) {
		this.applyUserCode = applyUserCode;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

	public String getIpWhiteList() {
		return ipWhiteList;
	}

	public void setIpWhiteList(String ipWhiteList) {
		this.ipWhiteList = ipWhiteList;
	}

	public String getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	public String getCreator() {
		return StringUtils.isEmpty(creator) ? Constants.DEFAULT_USER : creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreatedDate() {
		return createdDate == null ? new Date() : createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public String getModifier() {
		return StringUtils.isEmpty(modifier) ? Constants.DEFAULT_USER
				: modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	public Date getLastUpdatedDate() {
		return lastUpdatedDate;
	}

	public void setLastUpdatedDate(Date lastUpdatedDate) {
		this.lastUpdatedDate = lastUpdatedDate;
	}

	public int getCurApplyQty() {
		return curApplyQty;
	}

	public void setCurApplyQty(int curApplyQty) {
		this.curApplyQty = curApplyQty;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public Date getKeyInvalidDate() {
		return keyInvalidDate;
	}

	public void setKeyInvalidDate(Date keyInvalidDate) {
		this.keyInvalidDate = keyInvalidDate;
	}

	@Override
	public String toString() {
		return "Token [id=" + id + ", ifId=" + ifId + ", ifType=" + ifType
				+ ", ifName=" + ifName + ", ifCode=" + ifCode + ", ifVersion="
				+ ifVersion + ", appKey=" + appKey + ", keyInvalidDate="
				+ keyInvalidDate + ", pubSystemId=" + pubSystemId
				+ ", pubSystemCode=" + pubSystemCode + ", callSystemId="
				+ callSystemId + ", callSytemCode=" + callSytemCode
				+ ", linkTel=" + linkTel + ", applyDate=" + applyDate
				+ ", invalidTimes=" + invalidTimes + ", applyUserCode="
				+ applyUserCode + ", accessToken=" + accessToken
				+ ", ipWhiteList=" + ipWhiteList + ", isEnable=" + isEnable
				+ ", creator=" + creator + ", createdDate=" + createdDate
				+ ", modifier=" + modifier + ", lastUpdatedDate="
				+ lastUpdatedDate + ", curApplyQty=" + curApplyQty
				+ ", status=" + status + "]";
	}

}
